'''
Created on 28 sept. 2010
@author: Marc CARRE
@contact: carre.marc@gmail.com
@license: GNU GPLv3
@version: 1.0
'''

from SpacePartition import SpacePartition
from PointGenerator import PointGenerator
from FileWriter import FileWriter

class DataSetGenerator:
    
    def __init__(self, params, limits, spaces, numPoints, type, filePath):
        self.pointGen = PointGenerator(params, limits)
        self.type = type
        self.params = params
        
        self.spaces = []
        for (label, space) in spaces.items():
            self.spaces.append(SpacePartition(label, params, limits, space))
        
        self.numPoints = numPoints
        self.writer = FileWriter(filePath)

    def generate(self):
        if (self.type == "FLOAT"):
            self.generateFloat()
        elif (self.type == "INT"):
            self.generateInt()
            
    def generateFloat(self):
        if (type(self.numPoints) == int):
            self.generateFloatSimple()
        elif (type(self.numPoints) == list):
            self.generateFloatCategories()
            
    def generateFloatSimple(self):
        numPoints = 0
        while numPoints < self.numPoints:
            point = []
            for coord in self.params:
                point.append(self.pointGen.generateFloat(coord))
            for space in self.spaces:
                if (space.isInSpace(point)):
                    self.writer.write(point, space.label)
                    numPoints = numPoints + 1
                    continue
        self.writer.close()
                
    def generateFloatCategories(self):
        numPoints = [0] * len(self.numPoints)
        while numPoints < self.numPoints:
            point = []
            for coord in self.params:
                point.append(self.pointGen.generateFloat(coord))
            for space in self.spaces:
                idx = self.spaces.index(space)
                if (space.isInSpace(point)) and (numPoints[idx] < self.numPoints[idx]):
                    self.writer.write(point, space.label)
                    numPoints[idx] = numPoints[idx] + 1
                    continue
        self.writer.close()
        
    def generateInt(self):
        if (type(self.numPoints) == int):
            self.generateIntSimple()
        elif (type(self.numPoints) == list):
            self.generateIntCategories()
            
    def generateIntSimple(self):
        numPoints = 0
        while numPoints < self.numPoints:
            point = []
            for coord in self.params:
                point.append(self.pointGen.generateInt(coord))
            for space in self.spaces:
                if (space.isInSpace(point)):
                    self.writer.write(point, space.label)
                    numPoints = numPoints + 1
                    continue
        self.writer.close()
                
    def generateIntCategories(self):
        numPoints = [0] * len(self.numPoints)
        while numPoints < self.numPoints:
            point = []
            for coord in self.params:
                point.append(self.pointGen.generateInt(coord))
            for space in self.spaces:
                idx = self.spaces.index(space)
                if (space.isInSpace(point)) and (numPoints[idx] < self.numPoints[idx]):
                    self.writer.write(point, space.label)
                    numPoints[idx] = numPoints[idx] + 1
                    continue
        self.writer.close()